﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.ETL
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class CopyDbInstruction:InstructionBase
    {
        #region props
        private DataProviderType _SrcDbProvider;
        /// <summary></summary>
        public DataProviderType SrcDbProvider
        {
            get
            {
                return this._SrcDbProvider;
            }
            set
            {
                this._SrcDbProvider = value;
            }
        }

        private string _SrcConnStr;
        /// <summary></summary>
        public string SrcConnStr
        {
            get
            {
                return this._SrcConnStr;
            }
            set
            {
                this._SrcConnStr = value;
            }
        }

        private Dictionary<string,List<string>> _TablesToCopy;
        /// <summary></summary>
        public Dictionary<string,List<string>> TablesToCopy
        {
            get
            {
                return this._TablesToCopy;
            }
            set
            {
                this._TablesToCopy = value;
            }
        }

        private DataProviderType _TgtDbProvider;
        /// <summary></summary>
        public DataProviderType TgtDbProvider
        {
            get
            {
                return this._TgtDbProvider;
            }
            set
            {
                this._TgtDbProvider = value;
            }
        }

        private string _TgtConnStr;
        /// <summary></summary>
        public string TgtConnStr
        {
            get
            {
                return this._TgtConnStr;
            }
            set
            {
                this._TgtConnStr = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public CopyDbInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._SrcDbProvider = DataProviderType.MSSQL;
            this._SrcConnStr = string.Empty;
            this._TablesToCopy=new Dictionary<string, List<string>>();
            this._TgtDbProvider = DataProviderType.MSSQL;
            this._TgtConnStr = string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="srcDbProvider"></param>
        /// <param name="srcConnStr"></param>
        /// <param name="tblsToCopy"></param>
        /// <param name="tgtDbProvider"></param>
        /// <param name="tgtConnStr"></param>
        public CopyDbInstruction(Guid componentID, Guid jobTicketID,
            DataProviderType srcDbProvider, string srcConnStr,
            Dictionary<string, List<string>> tblsToCopy,
            DataProviderType tgtDbProvider, string tgtConnStr) 
            : base(componentID, jobTicketID)
        {
            this._SrcDbProvider = srcDbProvider;
            this._SrcConnStr = srcConnStr;
            this._TablesToCopy = tblsToCopy;
            this._TgtDbProvider = tgtDbProvider;
            this._TgtConnStr = tgtConnStr;
        }
        #endregion

        #region Overrides of InstructionBase

        /// <summary>
        /// serialize settings to xml
        /// the function below is mainly for storing into database as serialized form instead of string
        /// </summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"SrcDbProvider",this.SrcDbProvider.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcConnStr", this.SrcConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtDbProvider", this.TgtDbProvider.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtConnStr", this.TgtConnStr);
            XmlNode schemaNodes = XmlDataUtil.AddElement(ref xDoc, xNode, "Schemas");
            if(this.TablesToCopy !=null && this.TablesToCopy.Count>0)
            {
                foreach(string schemaName in this.TablesToCopy.Keys)
                {
                    XmlNode schemaNode = XmlDataUtil.AddElement(ref xDoc, schemaNodes, "Schema");
                    XmlDataUtil.UpdateAttribute(ref xDoc, schemaNode,"SchemaName", schemaName);
                    foreach(string tblName in this.TablesToCopy[schemaName])
                    {
                        XmlNode tblNode = XmlDataUtil.AddElement(ref xDoc, schemaNode, "Table");
                        XmlDataUtil.UpdateAttribute(ref xDoc, tblNode, "TableName",tblName);
                    }
                }
            }
            return xNode;
        }

        /// <summary>
        /// instantiated from xml
        /// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
        /// instantiate from first child of root node
        /// </summary>
        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                CopyDbInstruction instruction = new CopyDbInstruction();
                instruction.OwnerComponentID = new Guid(XmlDataUtil.GetAttributeValue(xNode, "ComponentID", Guid.NewGuid().ToString()));
                instruction.OwnerWorkerInstanceID = new Guid(XmlDataUtil.GetAttributeValue(xNode, "WorkerID", Guid.NewGuid().ToString()));
                instruction.SubmitTime = DateTime.Parse(XmlDataUtil.GetAttributeValue(xNode, "SubmitTime", TimeUtil.DbMinTime.ToString()));
                instruction.StartTime = DateTime.Parse(XmlDataUtil.GetAttributeValue(xNode, "StartTime", TimeUtil.DbMinTime.ToString()));

                instruction.SrcDbProvider = (DataProviderType) Enum.Parse(typeof (DataProviderType),
                                                                          XmlDataUtil.GetAttributeValue(xNode,
                                                                                                        "SrcDbProvider",
                                                                                                        DataProviderType
                                                                                                            .MSSQL.
                                                                                                            ToString()));
                instruction.SrcConnStr = XmlDataUtil.GetAttributeValue(xNode, "SrcConnStr", "");
                instruction.TgtDbProvider = (DataProviderType) Enum.Parse(typeof (DataProviderType),
                                                                          XmlDataUtil.GetAttributeValue(xNode,
                                                                                                        "TgtDbProvider",
                                                                                                        DataProviderType
                                                                                                            .MSSQL.
                                                                                                            ToString()));
                instruction.TgtConnStr = XmlDataUtil.GetAttributeValue(xNode, "TgtConnStr", "");
                instruction.TablesToCopy=new Dictionary<string, List<string>>();
                XmlNodeList schemaNodes = xNode.SelectNodes("Schemas/Schema");
                if(schemaNodes !=null && schemaNodes.Count>0)
                {
                    foreach(XmlNode schemaNode in schemaNodes)
                    {
                        string schemaName = XmlDataUtil.GetAttributeValue(schemaNode, "SchemaName", "");
                        List<string> tblNames=new List<string>();
                        foreach(XmlNode tblNode in schemaNode.ChildNodes)
                        {
                            tblNames.Add(XmlDataUtil.GetAttributeValue(tblNode, "TableName", ""));
                        }
                        instruction.TablesToCopy.Add(schemaName, tblNames);
                    }
                }
                
                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
